<template>
  <div class="right_main">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="流程名称" prop="processDefinitionName">
        <el-input
          v-model="queryParams.processDefinitionName"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请时间" prop="createTime">
        <el-date-picker
          style="width: 298px"
          v-model="createTime"
          type="datetimerange"
          range-separator="至"
          format="yyyy-MM-dd HH:mm"
          value-format="yyyy-MM-dd HH:mm"
          start-placeholder="开始日期"
          size="small"
          @keyup.enter.native="handleQuery"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="处理时间" prop="name">
        <el-date-picker
          style="width: 298px"
          v-model="overTime"
          type="datetimerange"
          range-separator="至"
          format="yyyy-MM-dd HH:mm"
          value-format="yyyy-MM-dd HH:mm"
          start-placeholder="开始日期"
          size="small"
          @keyup.enter.native="handleQuery"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="dataList"
      @selection-change="handleSelectionChange"
      height="7.4rem"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column fixed align="center" type="index" label="序号" width="50" />

      <el-table-column align="center" prop="processDefinitionName" label="流程名称" />
      <el-table-column align="center" prop="name" label="任务节点名称" sortable />
      <el-table-column align="center" prop="startTime" label="申请时间" sortable />
      <el-table-column align="center" prop="endTime" label="处理时间" sortable />
      <el-table-column align="center" prop="assignee" label="流程办理人" sortable />
      <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="clickTaskPop(scope.row)"
            size="mini"
            icon="el-icon-s-check"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog
      title="审批"
      width="80%"
      :visible.sync="dynamicFormEditVisible"
      append-to-body
      @closed="closeDynamicEdit"
    >
      <approvalForm
        ref="approvalForm"
        :businessKey="businessKey"
        :taskId="taskId"
        :parentTaskId="parentTaskId"
        :confirmVisible="false"
        @closeForm="closeDynamicEdit"
        :currProcessForm="currProcessForm"
        :processInstanceId="processInstanceId"
        :dynamicFormData="dynamicFormData"
      />
    </el-dialog>
  </div>
</template>

<script>
import api from "@/api/workflow/task";
import approvalForm from "@/views/components/approvalForm";
import { getBusinessForm } from "@/api/workflow/businessForm";
export default {
  components: {
    approvalForm
  },
  data() {
    return {
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 是否显示弹出层
      open: false,
      // 表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined
      },
      createTime: [],
      overTime: [],
      dataViewVisible: true,
      dynamicFormEditVisible: false,
      // 任务id
      taskId: undefined,
      taskVariables: undefined,
      processInstanceId: undefined,
      parentTaskId: undefined,
      businessKey: undefined, // 业务唯一标识
      currProcessForm: "", //表单组件名称
      dynamicFormData: "" //表单组件名称
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.createTime = [];
      this.overTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    //分页
    getList() {
      this.loading = true;
      if (this.createTime) {
        this.queryParams.runStartDate = this.createTime[0];
        this.queryParams.runEndDate = this.createTime[1];
      } else {
        this.queryParams.runStartDate = null;
        this.queryParams.runEndDate = null;
      }
      if (this.overTime) {
        this.queryParams.overStartDate = this.overTime[0];
        this.queryParams.overEndDate = this.overTime[1];
      } else {
        this.queryParams.overStartDate = null;
        this.queryParams.overEndDate = null;
      }
      api.getTaskFinishByPage(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //办理任务弹出层
    clickTaskPop(row) {
      this.businessKey = row.businessKey;
      this.processInstanceId = row.processInstanceId;
      this.taskId = row.id;
      this.parentTaskId = row.parentTaskId;
      if (
        row.actProcessDefSetting &&
        row.actProcessDefSetting.businessType === 0
      ) {
        getBusinessForm(this.businessKey).then(response => {
          this.dynamicFormData = response.data;
          this.dynamicFormEditVisible = true;
          this.dataViewVisible = false;
          this.currProcessForm = "dynamicFormEdit";
        });
      } else {
        this.currProcessForm = row.actProcessDefSetting.componentName;
        this.dynamicFormEditVisible = true;
        this.dataViewVisible = false;
      }
    },
    //关闭办理弹出层
    closeDynamicEdit() {
      this.dynamicFormEditVisible = false;
      this.dataViewVisible = true;
      this.getList();
    }
  }
};
</script>
<style scoped>
.right_main {
  height: 100%;
  width: 100%;
}
.form-container-header {
  height: 30px;
  padding-bottom: 10px;
}
.el-icon-close {
  float: right;
  cursor: pointer;
}
/* 设置滚动条宽度和高度 */
::v-deep .el-table__body-wrapper::-webkit-scrollbar {
  width: 8px; /* 横向滚动条 */
  height: 8px; /* 纵向滚动条 必写 */
}
/* 设置滚动条样式 */
::v-deep .el-table__body-wrapper::-webkit-scrollbar-thumb {
  background-color: #dde;
  border-radius: 3px;
}
</style>
